package com.easy.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easy.common.bean.Product;
import com.easy.common.service.IProductService;
import com.easy.common.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author Easy
 * @since 2025-09-09
 */
@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private IProductService productService;
@GetMapping("{id}")
    public Result getOne(@PathVariable("id") Integer id) {
        Product product = productService.getById(id);
        return Result.success(product);
    }
    @GetMapping("page")
    public Result page( Product product, Page page) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>(product);
        return Result.success(productService.page(page, queryWrapper));
    }
    @PostMapping
    public Result save(@RequestBody Product product) {
        return Result.success(productService.saveOrUpdate(product));
    }
    @DeleteMapping("{id}")
    public Result delete(@PathVariable("id") Integer id) {
        return Result.success(productService.removeById(id));
    }
    @GetMapping("all")
    public Result findAll() {
        return Result.success(productService.list());
    }
    @PutMapping
    public Result update(@RequestBody Product product) {
        return Result.success(productService.updateById(product));
    }

}
